package org.praisebase.pbpj.io;

import org.praisebase.pbpj.component.SongContainer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.praisebase.pbpj.component.*;
import org.praisebase.pbpj.component.SongAuthor.SongAuthorType;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


/**
 *
 * @author Nicolas Perrenoud <nicu@lavine.ch>
 */
public class OpenLyricsSongReader extends SongReader {
	
	public OpenLyricsSongReader() {
		
	}
	
	public SongContainer readSong(String source) {
	
		Song s = new Song();
		SongContainer sc = new SongContainer();

		XmlDomParser p = new XmlDomParser();
		try {

			Element docRoot = p.parseXmlFile(source);
	
			// Read format attributes
			sc.setSourceLocation(source);
			sc.setSourceFormat(SongSourceFormat.OpenLyrics);
			sc.setSourceFormatVersion(docRoot.getAttribute("version"));
			sc.setSourceGenerator(docRoot.getAttribute("createdIn"));
			
			// Iterate over properties
			NodeList propertiesNodeList = docRoot.getElementsByTagName("properties");
			if(propertiesNodeList != null && propertiesNodeList.getLength() > 0) {
				getSongProperties(s, (Element)propertiesNodeList.item(0));
			}
			
			// Iterate over lyrics
			NodeList lyricsNodeList = docRoot.getElementsByTagName("lyrics");
			if(lyricsNodeList != null && lyricsNodeList.getLength() > 0) {
				getSongLyrics(s, (Element)lyricsNodeList.item(0));
			}			
			
		} catch (ParserConfigurationException ex) {
			Logger.getLogger(OpenLyricsSongReader.class.getName()).log(Level.SEVERE, null, ex);
		} catch (SAXException ex) {
			Logger.getLogger(OpenLyricsSongReader.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IOException ex) {
			Logger.getLogger(OpenLyricsSongReader.class.getName()).log(Level.SEVERE, null, ex);
		}
		
		sc.setSong(s);
		return sc;			
	}	
	
	private void getSongProperties(Song s, Element propRoot) {

			// Get titles
			NodeList nl = propRoot.getElementsByTagName("titles");
			if(nl != null && nl.getLength() > 0) {
				NodeList snl = ((Element)nl.item(0)).getElementsByTagName("title");
				if(snl != null && snl.getLength() > 0) {
					for(int i = 0 ; i < snl.getLength();i++) {
						Element e = ((Element)snl.item(i));
						
						SongTitle st = new SongTitle();
						st.setTitle(e.getTextContent());
						if (!e.getAttribute("lang").isEmpty()) {
							st.setLanguage(e.getAttribute("lang"));
						}
						st.setOriginal(e.getAttribute("original").equals("true"));
						s.getTitles().add(st);
						
					}
				}
			}
		
			// Get authors
			nl = propRoot.getElementsByTagName("authors");
			if(nl != null && nl.getLength() > 0) {
				NodeList snl = ((Element)nl.item(0)).getElementsByTagName("author");
				if(snl != null && snl.getLength() > 0) {
					for(int i = 0 ; i < snl.getLength();i++) {						
						Element e = ((Element)snl.item(i));
						
						SongAuthor st = new SongAuthor();
						st.setAuthor(e.getTextContent());
						if (!e.getAttribute("lang").isEmpty()) {
							st.setLanguage(e.getAttribute("lang"));
						}
						if (!e.getAttribute("type").isEmpty()) {
							try {
								SongAuthorType t = SongAuthorType.valueOf(e.getAttribute("type"));
								st.setType(t);
							} catch (IllegalArgumentException ex) {}
						}						
						s.getAuthors().add(st);
						
					}
				}
			}
			
			s.setCopyright(XmlDomParser.getTextValue(propRoot, "copyright"));
			s.setCcliNumber(XmlDomParser.getIntValue(propRoot, "ccliNo"));
			s.setReleaseDate(XmlDomParser.getTextValue(propRoot, "released"));
			s.setTransposition(XmlDomParser.getIntValue(propRoot, "transposition"));
	}
	
	
	
	private void getSongLyrics(Song s, Element root) {
		
		
	}

}
